So in today's lesson we are going to talk about debugging.
So we're going to look all about errors and what to do when you get an error.
Okay.
So like with any programming language Lua is no different as you will get errors sometimes in your script.
Okay.
And we've mentioned this a couple times, but an error is basically when you have written something
incorrect or something that couldn't happen.
So you've made some kind of mistake when you were writing your code.
And when this mistake happens, the the script basically stops.
So if I open up a script here just to show you, say you also put a wait in there for two seconds.
And also, I don't know, um.
Printed something else as well.
Okay, so let's say the error is on line two.
Okay.
It will do line one.
Fine.
It will then get to the error and it will kind of stop there because it doesn't know what to do.
It doesn't just skip it, it completely stops there and won't read any the rest of your code.
So when you get an error, it's, um, you need to fix it because your script won't work properly unless
you don't, unless you do fix it.
So let's have a look what an error might look like.
Okay, so I'm just going to make a simple error purposely.
Um is equal to game dot workspace dot parts.
Okay.
So let's try to change the anchored property using a Vector3 value, which makes no sense.
Um, which is the point.
Okay, so let's run the game and see what happens.
Um.
Do be getting ever.
We don't.
Why don't we get an error for that?
That's really weird.
Okay.
Um, yeah.
Sometimes with, um, with this programming language.
Unlike other ones, because it's suited for people younger, um, wanting to learn some things that
wouldn't make sense.
Do make sense to PostScript for some reason.
Um, let's try instead.
Um.
Is equal to eight.
Surely that won't work.
That's really strange.
Why doesn't it work?
Let's try something else then.
This isn't too good.
Let's try.
Part dot position is equal to eight.
That definitely won't work.
Here we go.
Cool.
So yeah, some things that shouldn't work do work because it's a bit more of a forgiving programming
language, I guess.
Um, but anyway, we've got an error now, so it says unable to assign property position vector for
expected got number.
So I'm going to just talk a bit about what this actually tells you.
It tells you quite a lot about the error.
Okay.
First off it tells you the time that the error occurred or it it found the error which is just displayed
here.
Okay.
So this is in your local time.
So for me it was, it was at 158 that it occurred that the error occurred.
Even gives you a description of the error in red.
Okay.
So this here says unable to assign property position, which basically means it couldn't give the position
property a value.
This is because it expected a vector free value, but it got a number value basically.
So it was expecting a vector three.
You gave it a number.
This here tells you that it happened on the server and not on the client, and it also tells you this
is probably the most important bit.
Um, the name of the script which is error occurred in.
So it's called it happened in the script called script, which is our script here.
And then the colon free bit basically means occurred on line three.
So our error occurred in this script on line three.
So somewhere here okay.
Now fixing errors is something that you will just learn with practice.
Okay.
I can't tell you some secret hack which will never which will mean that you never get an error ever
again in your code.
That's not how this works.
It is just practice on how to fix them.
But there are certain ways to fix them that you will find a lot more useful.
So now we can we can probably get it down to a single line.
Now the line with the error occurred on is not always the line where the error is.
So for example, if you have a if you have made a variable here okay.
And you've made an error with what the variable is equal to, sometimes it will not find out that error
until you use your variable.
Okay.
So it's not forever is not always on that line.
It's just in that kind of region that it is in.
So just be careful with that.
Okay.
So we know the error is on line three.
And we've read through this and it says unable to assign property position vector expected got number.
So this is a relatively simple error.
But some other errors will be a bit difficult to understand what they mean.
And that's fine because they use quite advanced programming terms and language.
But what I'd recommend to you is just piece together the bits that you do know.
Okay.
So you know, there's a property called position.
And you can see here that we're trying to change the position property.
Okay.
You know what a vector free value is.
And you know what a number value is okay.
It said it expected vector three but it got a number.
We can see here that we've given it a number value.
But it expected a vector free value.
Which basically means you need to give it a vector free value instead.
Okay.
So vector free dot new 123 for example.
Okay.
So it's all about breaking down the error into some simple, simple parts that are wrong and then kind
of connect them all together and figuring out the solution.
Okay.
Um, so with all errors, you might not understand quite a lot of the words, but there will be a sound
that you definitely should understand which.
And if you don't yet, you will understand later on in the course.
So it's all about using your knowledge and applying what you what you already know to that so you can
fix the error.
Okay, um, now what happens if even after that you don't know where the error is?
Well, you can use a bit of a trick, which, um, I have figured out, um, which is let's let's write
down some stuff here.
So let's create a we've got the variable for a part.
Let's check if the part is transparent.
So if part dot transparency is equal one then we're going to change can collide to be false okay.
Else.
And we're going to change the material to neon.
M dot material dot neon.
Okay, so this little trick works based on the fact that when you get an error, your script will stop
reading.
Okay.
Which basically means if there is an error, um, on line two, for example, and line two isn't in
a loop or a variable or anything like that, we'll discuss what happens if they are in those in a minute.
It will not read any lines further down than line two.
So what you can do is to start off with print the letter A okay.
If the letter A prints, you know that the the script is being read.
Okay.
Um, if your first letter does not print, two things have happened.
Either you have, um, mistyped the print function or, um, your script could be disabled or it could
be in the wrong place.
Okay, so server scripts usually go either in the workspace or in server script service okay.
And when we go on to local scripts soon I'll just be discussing where they go as well.
Or you could be using a different type of script.
You could be doing something that should be done in local script.
A local script instead.
So if it doesn't print a, it's usually something wrong with the actual script object itself.
Okay.
Um, then after a bit of more stuff, print another letter.
So if A prints but B doesn't print, your error is in between those two parts here.
So it's got to be this variable here.
Okay.
Um now with if statements and loops and things like that, it gets a bit more difficult to do this.
Um, it's a bit more you need to think about.
Because if we also print C in here and print D in here and let's say print E at the end, just because
it doesn't print C does not mean our code is not working.
Okay.
Because if it doesn't print C the part could be transparent.
Sorry, the part might not be transparent, in which case it will run the else statement instead.
Okay.
So what you kind of need to check is should it run C so if it does print C, you need to make sure that
the transparency is equal to one.
And if it doesn't print C, you need to make sure that the transparency does not equal to one.
If it's supposed to print C and it doesn't.
You've now figured out where the issue is okay.
And let's say it prints a prince B, C, C or d and E.
So it prints all the letters.
Um, but there's but but so you haven't figured out an error yet.
Um, so if you've.
Yeah, if it prints all the letters, um, the issue is most likely a, not a syntax error, but a logic
error.
So there's those two types of errors.
What they mean is a syntax error is an error with something you typed.
So you misspelled something.
You give something a capital letter.
You put a dot where it shouldn't have gone, things like that okay.
A logic error means that your code works, but just not in the way that it's intended to.
Okay, so maybe it doesn't quite do what you want it to do, in which case these printing letters could
be useful as well as you could figure out.
It does what I want to do up until when it prints B, then it all goes wrong.
Okay.
Um, and other tools as well come in useful, but printing values and printing letters is always a really
good way to figure out what's going on.
For example, if you wanted to check what's going on with the if loop, um, you could print the parts
transparency like that there, which would then tell you whether it's printing C or printing D, for
example.
Okay.
So that's the best way to work around errors okay.
So next time you get an error, that's a kind of process that I would go and use.
But it's all about practice.
So it's not something you can kind of deliberately practice.
But as you code more and more you will encounter some errors, especially if you're doing a lot of practice
yourself, which is completely fine.
Hopefully now, um, you know a bit more about what to do and say.
For example, you've identified that you've made a mistake with the variable and you're not sure why.
Go and rewatch the lecture I put out on variables.
Um, and you can watch for it again and see if you did make a mistake, or if that's not the case,
have another look, but will always something the, um, the if you get an error, there will always
be something wrong with your script somewhere.
Okay.
You need to have a go and try and find it.
Okay?
So hopefully that gives you a good understanding of what to do when you get errors.
And it's all about practice for practice from here.
So continue on to the next lecture.
But if you do get any errors.
Hopefully you can handle them a bit better.